﻿using System;
using System.Linq;
using System.Web.UI.WebControls;


namespace LMS.SMSH.WEB
{
    public partial class Teacindex : System.Web.UI.Page
    {
        Int32 rowNo = 0; //行编号
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                // 绑定gvInfo数据
                bingGvInfo();
            }
        }

        /// <summary>
        /// 绑定gvInfo数据
        /// </summary>
        private void bingGvInfo()
        {
            var query = from info in LMSDataContext.GetLMSDB.TB_Info
                        select new
                        {
                            info.infoId,
                            info.infotitle,
                            info.infoType,
                            info.infoDetail
                        };
            gvInfo.DataSource = query;
            gvInfo.DataKeyNames = new string[] { "infoId" };
            gvInfo.DataBind();
        }

        /// <summary>
        /// 按照 字段infoType的值(1或者0)映射到界面成为"紧急通知"或者"普通通知"
        /// </summary>
        /// <param name="infoType"></param>
        /// <returns></returns>
        protected string GetgvInfoType(bool infoType)
        {
            return (infoType == true) ? "紧急通知" : "普通通知";
        }

        /// <summary>
        /// 根据信息标题、信息类别查找信息
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void btnFind_Click(object sender, EventArgs e)
        {
            bool infoType = (ddlInfo.SelectedValue == "1") ? true : false;
            if (!string.IsNullOrWhiteSpace(txtCaption.Text.Trim()))
            {
                var query = from info in LMSDataContext.GetLMSDB.TB_Info
                            where info.infotitle.IndexOf(txtCaption.Text.Trim()) >= 0 && info.infoType == infoType
                            select new
                            {
                                info.infoId,
                                info.infotitle,
                                info.infoType,
                                info.infoDetail
                            };
                gvInfo.DataSource = query;
                gvInfo.DataKeyNames = new string[] { "infoId" };
                gvInfo.DataBind();
            }
            else
            {
                bingGvInfo();
            }
        }



        /// <summary>
        /// 行数据绑定
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void gvInfo_RowDataBound(object sender, GridViewRowEventArgs e)
        {
            if (e.Row.RowType == DataControlRowType.DataRow)
            {
                rowNo++;
                e.Row.Cells[0].Text = (gvInfo.PageIndex * gvInfo.PageSize + rowNo).ToString();

                //鼠标悬停变色
                e.Row.Attributes.Add("onmouseover", "oldBG=this.style.backgroundColor;this.style.backgroundColor='#FE18e2';");
                e.Row.Attributes.Add("onmouseout", "this.style.backgroundColor=oldBG;");

              
            }
        }

        /// <summary>
        /// 分页
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void gvInfo_PageIndexChanging(object sender, GridViewPageEventArgs e)
        {
            gvInfo.PageIndex = e.NewPageIndex;
            bingGvInfo();
        }

        /// <summary>
        /// 紧急通知、临时通知相互变化
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void ddlInfo_SelectedIndexChanged(object sender, EventArgs e)
        {
            bool infoType = (ddlInfo.SelectedValue == "1") ? true : false;
            var query = from info in LMSDataContext.GetLMSDB.TB_Info
                        where info.infoType == infoType
                        select new
                        {
                            info.infoId,
                            info.infotitle,
                            info.infoType,
                            info.infoDetail
                        };
            gvInfo.DataSource = query;
            gvInfo.DataKeyNames = new string[] { "infoId" };
            gvInfo.DataBind();
        }




    }
}